package io.xxx.xbutler.core.event.job;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import io.xxx.xbutler.data.TaskMapper;
import io.xxx.xbutler.domain.task.Task;
import io.xxx.xbutler.domain.task.TaskStatus;
import jakarta.annotation.Nonnull;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.quartz.JobExecutionContext;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.time.LocalDateTime;

@Slf4j
public class TaskCheckExpireJob extends QuartzJobBean {

    @Resource
    private TaskMapper taskMapper;

    @Override
    protected void executeInternal(@Nonnull JobExecutionContext context) {
        LocalDateTime now = LocalDateTime.now();
        LambdaUpdateWrapper<Task> wrapper = new LambdaUpdateWrapper<Task>()
                .set(Task::getStatus, TaskStatus.EXPIRED)
                .set(Task::getUpdatedTime, now)
                .eq(Task::getStatus, TaskStatus.RUNNING)
                .lt(Task::getEndTime, now);
        taskMapper.update(wrapper);
    }
}
